package pannel.statistics;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.data.general.DefaultPieDataset;
import pannel.user.User;
import pannel.user.UserDao;
import system.BasePage;

import javax.swing.*;
import java.awt.*;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.Map;

public class UserStatisticsPage extends BasePage {
    private UserDao userDao;
    private JLabel totalUsersLabel;
    private ChartPanel chartPanel;

    public UserStatisticsPage(User user) {
        super(user);
    }

    @Override
    protected void initPageContent() {
        userDao = new UserDao();
        setPageTitle("用户统计");
        setupContent();
        refreshData();
    }

    private void setupContent() {
        contentArea.setLayout(new BorderLayout(20, 20));
        contentArea.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        // 顶部统计面板
        JPanel statsPanel = new JPanel();
        statsPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 20, 10));
        statsPanel.setBackground(Color.WHITE);
        
        // 总用户数统计卡片
        JPanel totalUsersCard = createStatCard("总用户数");
        totalUsersLabel = new JLabel("0");
        totalUsersLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
        totalUsersCard.add(totalUsersLabel);
        statsPanel.add(totalUsersCard);

        // 图表面板
        chartPanel = new ChartPanel(null);
        chartPanel.setPreferredSize(new Dimension(400, 400));

        // 布局
        contentArea.add(statsPanel, BorderLayout.NORTH);
        contentArea.add(chartPanel, BorderLayout.CENTER);

        // 添加刷新按钮
        JButton refreshButton = new JButton("刷新数据");
        refreshButton.addActionListener(e -> refreshData());
        contentArea.add(refreshButton, BorderLayout.SOUTH);
    }

    private JPanel createStatCard(String title) {
        JPanel card = new JPanel();
        card.setLayout(new BoxLayout(card, BoxLayout.Y_AXIS));
        card.setBackground(Color.WHITE);
        card.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(new Color(230, 230, 230), 1),
                BorderFactory.createEmptyBorder(15, 25, 15, 25)
        ));

        JLabel titleLabel = new JLabel(title);
        titleLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        titleLabel.setForeground(new Color(102, 102, 102));
        
        card.add(titleLabel);
        card.add(Box.createVerticalStrut(10));
        
        return card;
    }

    private void refreshData() {
        try {
            // 获取总用户数
            int totalUsers = userDao.getTotalCount(null);
            totalUsersLabel.setText(String.valueOf(totalUsers));

            // 获取角色分布数据
            Map<String, Integer> roleDistribution = userDao.getRoleDistribution();
            
            // 创建饼图数据集
            DefaultPieDataset dataset = new DefaultPieDataset();
            roleDistribution.forEach(dataset::setValue);

            // 创建饼图
            JFreeChart chart = ChartFactory.createPieChart(
                    "用户角色分布",
                    dataset,
                    true,
                    true,
                    false
            );

            // 设置字体
            Font defaultFont = new Font("微软雅黑", Font.PLAIN, 12);
            
            // 设置标题字体
            chart.getTitle().setFont(new Font("微软雅黑", Font.BOLD, 16));
            
            // 设置图表字体
            PiePlot plot = (PiePlot) chart.getPlot();
            plot.setLabelFont(defaultFont);
            plot.setLabelGenerator(new StandardPieSectionLabelGenerator(
                    "{0}: {1}人 ({2})", 
                    new DecimalFormat("0"),
                    new DecimalFormat("0.0%")
            ));
            
            // 设置图例字体
            chart.getLegend().setItemFont(defaultFont);
            
            // 更新图表
            chartPanel.setChart(chart);
            chartPanel.repaint();

        } catch (SQLException e) {
            JOptionPane.showMessageDialog(this, 
                    "获取统计数据失败：" + e.getMessage(), 
                    "错误", 
                    JOptionPane.ERROR_MESSAGE);
        }
    }
} 